<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form :model="pwdForm" :rules="pwdFormRules" ref="pwdFormRef" label-width="100px">
      <el-form-item label="原密码" prop="old_pwd" >
        <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getpassword">修改密码</el-button>
        <el-button @click="restpassword">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: 'UserPwd',
  data() {
      var validatePass = (rule, value, callback) => {
        if (value !== this.pwdForm.old_pwd) {
          callback()
        } else {
         callback(new Error('请输入密码'));
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === this.pwdForm.new_pwd) {
          callback()
        } else {
         callback(new Error('密码不一致'));
        }
      };
    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      // 表单的验证规则对象
      pwdFormRules: {
          old_pwd:[
               { required: true, message: '请输入原密码', trigger: 'blur' },
               { min: 6, max: 15, message: '长度在 6 到 5 个字符', trigger: 'blur' }
          ],
          new_pwd:[
               { required: true, message: '请输入新密码', trigger: 'blur' },
               { min: 6, max: 15, message: '长度在 6 到 5 个字符', trigger: 'blur' },
               { validator: validatePass, trigger: 'blur' }

          ],
          re_pwd:[
               { required: true, message: '请输入确认密码', trigger: 'blur' },
               { min: 6, max: 15, message: '长度在 6 到 5 个字符', trigger: 'blur' },
                { validator: validatePass2, trigger: 'blur' }
          ],
      }
    }
  },
  methods:{
    //   重置 相当于清空
      restpassword(){
           this.$refs.pwdFormRef.resetFields();
      },
    //   修改数据
    // 兜底
    getpassword(){
        this.$refs.pwdFormRef.validate(async (valid)=>{
            console.log(valid);
            if(!valid)return
            const {data:res} = await this.$axios.patch('/my/updatepwd',this.pwdForm)
            console.log(res); // 密码8个1
            this.$refs.pwdFormRef.resetFields();
        })
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>